package com.example.testoink;

import java.util.Timer;
import java.util.TimerTask;

import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class Enemy {
	float xEnemy;
	float yEnemy;
	TranslateAnimation walkToGoal;
	RelativeLayout workingLayout;
	MainActivity workingActivity;
	int enemyId;
	ImageView img1; 
	AnimationDrawable mAnimation;
	Rect otherViewRect1;
    Timer  logicTimer;
    
    BitmapDrawable[] frame;
	public Rect getEnemyView(){
		//Rect otherViewRect1 = new Rect();
   		img1.getHitRect(otherViewRect1);
   		return otherViewRect1;
		
	}
	public Enemy(RelativeLayout layout,MainActivity activity,int type){
	
		workingLayout = layout;
		workingActivity = activity;
		otherViewRect1 = new Rect();
	}
	
	public float getXEnemy(){
		
		return xEnemy;
		
	}
	
	public float getYEnemy(){
		return yEnemy;
		
	}
	
	public void startWalking(){
		
	
	    
		frame = new BitmapDrawable[9];
		//int i=0;
		
		for(int i=0; i<4; i++){			
			frame[i] = (BitmapDrawable)workingActivity.getResources().getDrawable( 
					workingActivity.getResources().getIdentifier((String)"enemy"+(i+1), "drawable",workingActivity.getPackageName()) );
		}
		
        int reasonableDuration = 200;
        mAnimation = new AnimationDrawable();                
        for(int i=0; i<4; i++){
        	mAnimation.addFrame(frame[i], reasonableDuration);
        }
      
        img1.setImageDrawable(mAnimation);
        //mAnimation.start();
        mAnimation.setOneShot(false);
        //mAnimation.set
        //img1.startAnimation(animation);
        
    	walkToGoal = new TranslateAnimation(0,1600,0,0);
		walkToGoal.setDuration(8000);
		//walkToGoal.set
		walkToGoal.setRepeatCount(0);
		walkToGoal.setFillAfter(true);
		
		 walkToGoal.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
				// RelativeLayout.LayoutParams rootlp = (RelativeLayout.LayoutParams) workingLayout.getLayoutParams();
	      
				//
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				
				goodbye();
				
			}
		}) ;
	    img1.startAnimation(walkToGoal);
		
	}
	
	public void drawOnLayout(int id,int xPos,int yPos){
		
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(1920/10,1028/5);
		img1 = new ImageView(workingActivity);
		img1.setImageResource( R.drawable.enemy1);
		img1.setX(xPos);
		img1.setY(yPos+60);
		//img1.setId(id);
		enemyId = id;
		workingLayout.addView(img1, params);
		xEnemy = img1.getX();
		startWalking();
		enemyLogic();
    }
	
	public void goodbye(){
		
		workingActivity = null;
		workingLayout = null;
		
		System.out.println("Sas");
		
		//img1.setX(1600);
		img1.setImageDrawable(null);
		mAnimation.stop();
		img1.setAnimation(null);
	    //mAnimation = null;
		 
		logicTimer.cancel();
		//enemyLogic.
		walkToGoal.cancel();
		
		//Toast.makeText(workingActivity.getApplicationContext(),"goodbye"+enemyId, 
	   		//	   Toast.LENGTH_LONG).show();
		//img1.setVisibility(View.INVISIBLE);
		//img1.setVisibility(View.GONE);

	
		
		//img1 =null :
		// ((MainActivity)img1.getParent()).removeView(img1); 
	
		//workingActivity.removeEnemy(this);
		
	}
	
	public void enemyLogic(){
		
		 logicTimer = new Timer();	

		logicTimer.scheduleAtFixedRate(new TimerTask() {
	
			public void run() {
	            workingActivity.runOnUiThread(new Runnable() //run on ui thread
	             {

	              public void run() 
	              {
	            	 // img1.getHitRect(otherViewRect1);
	            xEnemy = (float) (xEnemy+20);
	            yEnemy = img1.getY();
	           
	            	  if(walkToGoal.hasEnded()){
	            		  
	            		  walkToGoal.cancel();
	            		  
	            		  logicTimer.cancel();
	            		  //img1.setX((float) (img1.getX()+20.66));
	            		  
	            		 // System.out.println(img1.getX());
	            		  
	            		  workingActivity.updateHeartBy(-1);
	            		 
	            		  
	            		  
	            		  goodbye();
	            		  
	            		 
	            		  
	            		 
	            		  
	            		  //System.out.println(img1.getX());
	            		  
	            		
	            	  }
	            	  
	              }
	             });
	        }
	    
			},100,100);
		
		
		
	}

}
